<?php
//登录相关  
namespace NewAdmin\Controller;
use Think\Controller;
class UserController extends Controller {
    public function login(){
        if( session('user_id') ){
            urlSkip('您已登录',ADMIN_ROOT);
        }
        $this->assign('title','用户登录');
		$this->display('login2');
    }
    
    public function ajax_r($code='0',$data='',$message='') {
		$msg['code']=$code;
		$msg['data']=$data;
		$msg['message']=$message;
	 	$this->ajaxReturn($msg);
	}
	
    public function loginin(){
        $user   =   D('User');
    
        if( !I('post.username') || !I('post.password') ){
        	$this->ajax_r(0,'','请输入用户名和密码！');
        }
        $userArrayFlag	=	$user->field('id')->where(' user_name = "'.I('post.username').'" AND ishidden = 0')->find();
        if( !$userArrayFlag ){
        	$this->ajax_r(0,'','未查询到该用户信息！');
        }
        $userArray  =   $user->field('*')->where(' user_name = "'.I('post.username').'" AND password = "'.md5(I('post.password').'ltj2016').'" AND ishidden = 0' )->find();
   
        
        if( $userArray ){
        	session('user_id',$userArray['id']);
            session('nickname',$userArray['nickname']);
            session('isadmin',$userArray['isadmin']);
            session('buser_id',$userArray['id']);
            $url    =   $_POST['loginurl']?$_POST['loginurl']:'/Index/index';
            $this->ajax_r(1,$url,'登录成功！');
        }else{
        	 $this->ajax_r(0,'','密码错误！');
        }
    }
    
    public function loginExit(){
        session('user_id','0');
        session('nickname','0');
        session('isadmin','0');
        echo '4|||退出成功|||/User/login';die();
    }
	/*
	 * 商品图片集
	 */
	public function uploadify()
	{
		if(I('get.isthumb')){
			$width	=	I('get.width','400');
			$height	=	I('get.height','400');
		}else{
			$width	=	I('get.width');
			$height	=	I('get.height');
		}
		if (!empty($_FILES)) {
			import("@.ORG.UploadFile");
			$upload = new \Think\Upload();
			$upload->maxSize = 2048000;
			$upload->allowExts = array('jpg','jpeg','gif','png','xls');
			$upload->savePath = "/";
			$upload->thumb = true; //设置缩略图
			$upload->imageClassPath = "@.ORG.Image";
			$upload->saveRule = uniqid; //上传规则
			$upload->thumbRemoveOrigin = true; //删除原图
			$info   =   $upload->upload();
			if(!$info) {
				echo 'error'.$upload->getError();// 上传错误提示错误信息
			}else{
				foreach($info as $file){
					$path='/Uploads'.$file['savepath'].$file['savename'];
        			$name	=	$_SERVER["DOCUMENT_ROOT"].$path;
			        $imageArray	=	getimagesize($name);
			        if( ( $width || $height ) && ( $imageArray[0] > $width || $imageArray[1] > $height ) ){
						$save_path=$_SERVER['DOCUMENT_ROOT'].$path;
						$thumb_path=$_SERVER['DOCUMENT_ROOT'].'/Uploads'.$file['savepath'].'thumb_'.$file['savename'];
						$image = new \Think\Image(); 
						$image->open($save_path);
						$image->thumb($width, $height)->save($save_path);
			        }
					echo $path;
				}
			}
		}
	}

    public function uploadify_wb()
    {
       
       
        if (!empty($_FILES)) {
            import("@.ORG.UploadFile");
            $upload = new \Think\Upload();
            $upload->maxSize = 2048000;
            $upload->allowExts = array('jpg','jpeg','gif','png','xls');
            $upload->savePath = "/";
            $upload->thumb = true; //设置缩略图
            $upload->imageClassPath = "@.ORG.Image";
            $upload->saveRule = uniqid; //上传规则
            $upload->thumbRemoveOrigin = true; //删除原图
            $info   =   $upload->upload();
            if(!$info) {
                echo 'error'.$upload->getError();// 上传错误提示错误信息
            }else{
                foreach($info as $file){
                    $path='/Uploads'.$file['savepath'].$file['savename'];
                    $name   =   $_SERVER["DOCUMENT_ROOT"].$path;
                    $imageArray =   getimagesize($name);
                    if( ( $width || $height ) && ( $imageArray[0] > $width || $imageArray[1] > $height ) ){
                        $save_path=$_SERVER['DOCUMENT_ROOT'].$path;
                        $thumb_path=$_SERVER['DOCUMENT_ROOT'].'/Uploads'.$file['savepath'].'thumb_'.$file['savename'];
                        $image = new \Think\Image(); 
                        $image->open($save_path);
                        $image->thumb($width, $height)->save($save_path);
                    }
                    
                }
            }
            $data['saveURL']=$path;
            $data['state']='success';
            $data['status']=1;
            $this->ajaxReturn($data);

        }
    }
}